/*
** escaping.c for minishell2 in /home/guina_b/
** 
** Made by benoit guina
** Login   <guina_b@epitech.net>
** 
** Started on  Fri Jan 01 00:00:00 2010 benoit guina
** Last update Fri Jan 01 00:00:00 2010 benoit guina
*/

#include "escaping.h"

int is_backslashed(char *s, int c)
{
  int n;

  if (is_simplequoted(s, c))
    return (0);
  n = 0;
  while (--c >= 0 && s[c] == '\\')
    n++;
  return (n % 2);
}

int is_simplequoted(char *s, int c)
{
  int i;
  int q;

  q = 0;
  i = -1;
  while (s[++i] && i < c)
    if (s[i] == '\'')
      q = (!q && !is_backslashed(s, i) && !is_doublequoted(s, c)) ? 1 : 0;
  return (q);
}

int is_doublequoted(char *s, int c)
{
  int i;
  int q;

  q = 0;
  i = -1;
  while (s[++i] && i < c)
    if (s[i] == '"' && !is_backslashed(s, i))
      q = !q;
  return (q);
}

e_escaping is_escaped(char *s, int c)
{
  if (is_simplequoted(s, c))
    return (ESCAPING_SIMPLE);
  if (is_backslashed(s, c))
    return (ESCAPING_BACKSLASH);
  if (is_doublequoted(s, c))
    return (ESCAPING_DOUBLE);
  return (ESCAPING_NONE);
}

int is_escaping(char *s, int c)
{
  if (!(s[c] == '\\' || s[c] == '\'' || s[c] == '"'))
    return (0);
  return (!(is_backslashed(s, c) || (is_simplequoted(s, c) && s[c] != '\'')));
}
